
#include "indexarraymapping.hxx"


IndexArrayMapping::IndexArrayMapping(uint32_t size, uint32_t sectionSize) 
: m_size(size), m_sectionSize(sectionSize) {

    uint32_t n = (size + sectionSize - 1)/sectionSize;
    m_buflen = 1 + (n >> 3);
    m_base = new uint8_t[m_buflen];
    m_shift = 0;
    for(uint32_t s = 1, i = 0; i < sizeof(uint32_t) * 8; ++i) {
    
        if(s & sectionSize) m_shift = i;
        s = (s << 1);
    }
}

IndexArrayMapping::~IndexArrayMapping() {

    if(m_base) delete[] m_base;
    m_base = 0;
}

void IndexArrayMapping::insert(uint32_t index) {

    uint32_t i = (index >> m_shift);
    uint32_t k = (i >> 3);
    uint32_t offset = i & 0x7;

    m_base[k] |= (1 << (7 - offset));
}


bool IndexArrayMapping::test(uint32_t index) {

    uint32_t i = (index >> m_shift);
    uint32_t k = (i >> 3); 
    uint32_t offset = i & 0x7;

    return m_base[k] & (1 << (7 - offset));
}


uint32_t IndexArrayMapping::getSectionSize() {

    return m_sectionSize;
}

//assume iam has the same size as the object
void IndexArrayMapping::merge(const IndexArrayMapping& iam) {

    for(int i = 0; i < m_buflen; ++i) {
    
        m_base[i] |= iam.m_base[i];
    }
}


